package com.wind.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.debug.soblogin.R;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class ArticleActivity extends AppCompatActivity {
    private WebView webView;
    private TextView mTitle;
    private TextView mAuthor;
    private TextView mViewCount;
    private TextView mTime;
    private TextView mLabel;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.article_content);
        webView = findViewById(R.id.webview);
        intiView();
        initData();
    }

    private void intiView() {
        mTitle = findViewById(R.id.article_title);
        mAuthor = findViewById(R.id.article_author);
        mViewCount = findViewById(R.id.article_view_count);
        mTime = findViewById(R.id.article_create_time);
        mLabel = findViewById(R.id.article_label);
    }


    @SuppressLint("SetJavaScriptEnabled")
    private void initData() {

        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        String article_title = intent.getStringExtra("article_title");
        String time = intent.getStringExtra("time");
        String viewCount = intent.getStringExtra("viewCount");
        String label = intent.getStringExtra("label");
        if (label.contains("-")) {
            String[] split = label.split("-");
            label = split[0];
        }
        mTitle.setText(article_title);
        mTime.setText(time);
        mViewCount.setText(viewCount);
        mAuthor.setText("依依东望");
        mLabel.setText(label);

        try {//本地HTML里面有跨域的请求 原生webview需要设置之后才能实现跨域请求
            Class<?> clazz = webView.getSettings().getClass();
            Method method = clazz.getMethod(
                    "setAllowUniversalAccessFromFileURLs", boolean.class);
            method.invoke(webView.getSettings(), true);
        } catch (IllegalArgumentException | NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
        }
        webView.getSettings().setJavaScriptEnabled(true);
        webView.loadUrl("file:///android_asset/vue/index.html?id="+ id);
        //webView.loadUrl("https://www.lolxiaoguo.cn/ly");
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
                return super.shouldOverrideUrlLoading(view, request);
            }
        });

    }
}
